Use constructor pattern for `project_layout` in `toml.rs`.
authorCorey Farwell <coreyf@rwell.org>
Tue, 3 May 2016 00:59:39 +0000 (20:59 -0400)
committerCorey Farwell <coreyf@rwell.org>
Tue, 3 May 2016 00:59:39 +0000 (20:59 -0400)
src/cargo/ops/cargo_read_manifest.rs
src/cargo/util/toml.rs

index 445a356c42682273ae6b407b8794208e9cc0f3d2..56ae6a5e3338e911d5324f6e10b2601d51bd5551 100644 (file)
@@ -6,7 +6,7 @@ use std::path::{Path, PathBuf};
 use core::{Package, Manifest, SourceId, PackageId};
 use util::{self, paths, CargoResult, human, Config, ChainError};
 use util::important_paths::find_project_manifest_exact;
-use util::toml::{Layout, project_layout};
+use util::toml::Layout;
 
 pub fn read_manifest(contents: &[u8], layout: Layout, source_id: &SourceId,
                      config: &Config)
@@ -23,7 +23,7 @@ pub fn read_package(path: &Path, source_id: &SourceId, config: &Config)
     trace!("read_package; path={}; source-id={}", path.display(), source_id);
     let data = try!(paths::read(path));
 
-    let layout = project_layout(path.parent().unwrap());
+    let layout = Layout::from_project_path(path.parent().unwrap());
     let (manifest, nested) =
         try!(read_manifest(data.as_bytes(), layout, source_id, config));
 
index 191e48b34e6fc2ecaca518b5a60218a50f2fa29e..d0b2fe17f33b5a0d1f853f91cdd3bf83f88654aa 100644 (file)
@@ -32,6 +32,38 @@ pub struct Layout {
 }
 
 impl Layout {
+    /// Returns a new `Layout` for a given root path.
+    /// The `root_path` represents the directory that contains the `Cargo.toml` file.
+    pub fn from_project_path(root_path: &Path) -> Layout {
+        let mut lib = None;
+        let mut bins = vec![];
+        let mut examples = vec![];
+        let mut tests = vec![];
+        let mut benches = vec![];
+
+        let lib_canidate = root_path.join("src").join("lib.rs");
+        if fs::metadata(&lib_canidate).is_ok() {
+            lib = Some(lib_canidate);
+        }
+
+        try_add_file(&mut bins, root_path.join("src").join("main.rs"));
+        try_add_files(&mut bins, root_path.join("src").join("bin"));
+
+        try_add_files(&mut examples, root_path.join("examples"));
+
+        try_add_files(&mut tests, root_path.join("tests"));
+        try_add_files(&mut benches, root_path.join("benches"));
+
+        Layout {
+            root: root_path.to_path_buf(),
+            lib: lib,
+            bins: bins,
+            examples: examples,
+            tests: tests,
+            benches: benches,
+        }
+    }
+
     fn main(&self) -> Option<&PathBuf> {
         self.bins.iter().find(|p| {
             match p.file_name().and_then(|s| s.to_str()) {
@@ -69,39 +101,6 @@ fn try_add_files(files: &mut Vec<PathBuf>, root: PathBuf) {
     }
 }
 
-/// Returns a new `Layout` for a given root path.
-/// The `root_path` represents the directory that contains the `Cargo.toml` file.
-
-pub fn project_layout(root_path: &Path) -> Layout {
-    let mut lib = None;
-    let mut bins = vec![];
-    let mut examples = vec![];
-    let mut tests = vec![];
-    let mut benches = vec![];
-
-    let lib_canidate = root_path.join("src").join("lib.rs");
-    if fs::metadata(&lib_canidate).is_ok() {
-        lib = Some(lib_canidate);
-    }
-
-    try_add_file(&mut bins, root_path.join("src").join("main.rs"));
-    try_add_files(&mut bins, root_path.join("src").join("bin"));
-
-    try_add_files(&mut examples, root_path.join("examples"));
-
-    try_add_files(&mut tests, root_path.join("tests"));
-    try_add_files(&mut benches, root_path.join("benches"));
-
-    Layout {
-        root: root_path.to_path_buf(),
-        lib: lib,
-        bins: bins,
-        examples: examples,
-        tests: tests,
-        benches: benches,
-    }
-}
-
 pub fn to_manifest(contents: &[u8],
                    source_id: &SourceId,
                    layout: Layout,